package com.java8.zhangzixiang;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;

public class Demo01 {
  public static void main(String[] args) {
    List<Student> list = new ArrayList<>();
    Student s1 = new Student();
    s1.setId(1);
    s1.setName("kfds");
    s1.setAge(22);
    list.add(s1);

    Student s2 = new Student();
    s1.setId(2);
    s1.setName("ks");
    s1.setAge(24);
    list.add(s2);

    Student s3 = new Student();
    s1.setId(3);
    s1.setName("kfd");
    s1.setAge(21);
    list.add(s3);

    for (Student s:list){
      System.out.println(s);
    }
//nameda表达式

    //排序list sort Comparator
    list.sort(new Comparator<Student>() {
      @Override
      public int compare(Student o1, Student o2) {
        return o1.getAge() - o2.getAge();
      }
    });//{}中匿名内部类实现接口
    //匿名内部类替换成 lambda表达式
    list.sort((o1, o2) -> o1.getAge() - o2.getAge());//可以手动指定类

    //循环输出list forEach Consumer
    list.forEach(new Consumer<Student>() {
      @Override
      public void accept(Student s) {
        System.out.println(s);
      }
    });
    //匿名内部类替换成 lambda表达式
    list.forEach(s -> System.out.println(s));

  }
}
